package com.neusoft.neuiotms.dcom.controller.hn;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.dto.result.PageResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultDTO;
import com.neusoft.bizcore.web.dto.result.ResultListDTO;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.dto.hn.HnInspectionHealthPlanDTO;
import com.neusoft.neuiotms.dcom.dto.hn.HnInspectionItemDTO;
import com.neusoft.neuiotms.dcom.service.hn.HnInspectionHealthPlanService;
import com.neusoft.neuiotms.dcom.task.hn.HnInspectionHealthTaskGenService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "海南项目定期健康巡检计划管理")
@RestController
@RequestMapping(value = "/api/w/hn/inspection/plan/health")
public class HnInspectionHealthPlanController {
    @Autowired
    private HnInspectionHealthPlanService hnInspectionPlanService;
    @Autowired
    private HnInspectionHealthTaskGenService hnInspectionHealthTaskGenService;

    @ApiOperation("查询所有定期健康巡检计划")
    @GetMapping
    public ResultListDTO<HnInspectionHealthPlanDTO> index(final Searchable searchable) {
        return ResultListDTO.success(this.hnInspectionPlanService.index(searchable));
    }

    @ApiOperation("分页查询所有定期健康巡检计划")
    @GetMapping("/p")
    public PageResultDTO<HnInspectionHealthPlanDTO> search(final Pageable pageable, final Searchable searchable) {
        return PageResultDTO.success(this.hnInspectionPlanService.search(pageable, searchable));
    }

    @ApiOperation("创建定期健康巡检计划")
    @PostMapping("")
    public ResultDTO<HnInspectionHealthPlanDTO> create(@RequestBody final HnInspectionHealthPlanDTO dto,
            final UserBean user) {
        return ResultDTO.success(this.hnInspectionPlanService.create(dto, user));
    }

    @ApiOperation(value = "修改定期健康巡检计划")
    @PutMapping("/{id}")
    public ResultDTO<HnInspectionHealthPlanDTO> update(@PathVariable final Long id,
            @RequestBody final HnInspectionHealthPlanDTO dto, final UserBean user) {
        return ResultDTO.success(this.hnInspectionPlanService.update(id, dto, user));
    }

    @ApiOperation("定期健康巡检计划详情")
    @GetMapping("/{id}")
    public ResultDTO<HnInspectionHealthPlanDTO> details(@PathVariable final Long id) {
        return ResultDTO.success(this.hnInspectionPlanService.details(id));
    }

    @ApiOperation("定期健康删除巡检计划")
    @DeleteMapping("/{id}")
    public ResultDTO<Void> delete(@PathVariable final Long id) {
        this.hnInspectionPlanService.delete(id);
        return ResultDTO.success();
    }

    @ApiOperation("根据厅局code查询巡检项")
    @DeleteMapping("institutionCode/{code}")
    public ResultListDTO<HnInspectionItemDTO> delete(@PathVariable final String code) {
        return ResultListDTO.success(this.hnInspectionPlanService.findByInstitutionCode(code));
    }

    @ApiOperation("启用定时巡检任务")
    @PutMapping("/{id}/enable")
    public ResultDTO<Void> enable(@PathVariable final Long id) {
        this.hnInspectionPlanService.enable(id);
        return ResultDTO.success();
    }

    @ApiOperation("禁用定时巡检任务")
    @PutMapping("/{id}/disable")
    public ResultDTO<Void> disable(@PathVariable final Long id) {
        this.hnInspectionPlanService.disable(id);
        return ResultDTO.success();
    }

    /**
     * http://localhost:8000/api/w/hn/inspection/plan/3/tasktest
     *
     * @param id
     * @return
     */
    @ApiOperation("健康巡检任务生成测试")
    @GetMapping("/{id}/tasktest")
    public ResultDTO<?> taskTest(@PathVariable final Long id) {
        this.hnInspectionHealthTaskGenService.executeInternal(id);
        return ResultDTO.success();
    }

}
